﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using Pixysoft.Framework.Logs.Controller;

namespace Pixysoft.Framework.Logs.Core
{
    class LogDocument : ILogDocument
    {
        IXmlNode node;

        public LogDocument(IXmlNode node)
        {
            this.node = node;
        }

        public string RawSerialize()
        {
            return LogNonStructureSerializeController.Instance.Serialize(this);
        }

        public string Serialize()
        {
            return Serialize(false);
        }

        public string Serialize(bool indent)
        {
            return node.Serialize(indent);
        }

        public ILogRecordCollection Records
        {
            get
            {
                return new LogRecordCollection(node.Nodes[StringHelper.default_element_log]);
            }
            set
            {
                this.node.Nodes.Remove(StringHelper.default_element_log);

                if (value == null)
                    return;

                foreach (ILogRecord record in value)
                {
                    this.Records.Add(record);
                }
            }
        }

        public string Filepath
        {
            get
            {
                if (!node.Node.Contains(StringHelper.default_element_filepath))
                    return null;

                return node.Node[StringHelper.default_element_filepath].Text;
            }
            set
            {
                node.Node.Remove(StringHelper.default_element_filepath);

                if (string.IsNullOrEmpty(value))
                    return;

                node.Node.Add(StringHelper.default_element_filepath, value);
            }
        }

        public string Filename
        {
            get
            {
                if (!node.Node.Contains(StringHelper.default_element_filename))
                    return null;

                return node.Node[StringHelper.default_element_filename].Text;
            }
            set
            {
                node.Node.Remove(StringHelper.default_element_filename);

                if (string.IsNullOrEmpty(value))
                    return;

                node.Node.Add(StringHelper.default_element_filename, value);
            }
        }
    }
}
